from flask import jsonify,Blueprint,g,Response
from flask_restful import Api,Resource,reqparse,marshal

from werkzeug.security import generate_password_hash,check_password_hash
from common.utils.login_utils import *

from common.models import db
from common.models.users import *
from common.models.coures import *
from common.coures_fileds.course import *
from common.coures_fileds.user_fields import *

import json


comment_bp = Blueprint("comment_bp",__name__,url_prefix="/comment")

api = Api(comment_bp)



# 课程的评论信息
class CourseCommentResource(Resource):
    # 获取评论信息
    def get(self):
        try:
            parse = reqparse.RequestParser()
            parse = parse.add_argument("course_id")
            args = parse.parse_args()
            course_id = args.get("course_id")

            comment = CommentModel.query.filter_by(course_id=course_id).all()
            comment = marshal(comment, comment_fields)
            comment = json.loads(json.dumps(comment))

            for i in comment:
                user = User.query.filter_by(uid=i["user_id"]).first()
                user = marshal(user, user_fields)

                i["user_info"] = user
                parent_comment = CommentModel.query.filter_by(parent_id=i["id"]).all()
                parent_comment = marshal(parent_comment, comment_fields)
                parent_comment = json.loads(json.dumps(parent_comment))

                for a in parent_comment:
                    user = User.query.filter_by(uid=a["user_id"]).first()
                    user = marshal(user, user_fields)
                    a["user_info"] = user
                i["childlist"] = parent_comment
            return jsonify(message="ok！", code=200, data=comment)
        except Exception as e:
            print(e)
            return jsonify(message="服务器错误！", code=500)

    # 发表回复评论
    def post(self):
        try:
            parse = reqparse.RequestParser()
            parse = parse.add_argument("content")
            parse = parse.add_argument("ques_id")
            parse = parse.add_argument("reply")
            parse = parse.add_argument("user_id")
            args = parse.parse_args()
            content = args.get("content")
            cid = args.get("ques_id")
            parent_id = args.get("reply")
            user_id = args.get("user_id")
            # user_id = g.user_id
            print("数据》》》》》》》》》》》》》》》》", content)
            print("数据》》》》》》》》》》》》》》》》", user_id)
            print("数据》》》》》》》》》》》》》》》》", parent_id)
            print("数据》》》》》》》》》》》》》》》》", cid)

            if cid != None:
                comment = CommentModel(user_id=user_id,course_id=cid,content=content)
            else:
                comment = CommentModel(user_id=user_id, parent_id=parent_id, content=content)

                get_comment = CommentModel.query.filter_by(id=parent_id).first()
                if not get_comment:
                    return jsonify(message="评论不存在", code=401)

                CommentModel.query.filter_by(id=parent_id).update({"reply_count":get_comment.reply_count + 1})
                db.session.commit()

            db.session.add(comment)
            db.session.commit()
            return jsonify(message="成功", code=200, )
        except Exception as e:
            print(e)
            return jsonify(message="服务器错误", code=500)


# 评论点赞
class CommentLikeResource(Resource):
    def post(self):
        try:
            parse = reqparse.RequestParser()
            parse = parse.add_argument("com_id")
            parse = parse.add_argument("is_favorite")
            args = parse.parse_args()
            com_id = args.get("com_id")
            is_favorite = args.get("is_favorite")
            print("验证数据是否存在com_id",com_id)
            print("验证数据是否存在is_favorite",is_favorite)

            comment = CommentModel.query.filter_by(id=com_id).first()
            if not comment:
                return jsonify(message="该评论不存在！", code=401)

            up_comment = CommentModel.query.filter_by(id=com_id)
            # 更新点赞量
            if is_favorite == "True":
                up_comment.update({"like_count":comment.like_count + 1, "collect": 0})
            else:
                up_comment.update({"like_count": comment.like_count - 1, "collect": 1})
            db.session.commit()
            return jsonify(message="ok！", code=200, is_favorite=is_favorite)
        except Exception as e:
            print(e)
            return jsonify(message="服务器错误！", code=500)


class CollectResource(Resource):

    def post(self, pk):
        parse = reqparse.RequestParser()
        parse = parse.add_argument("id")
        args = parse.parse_args()
        id = args.get("id")
        print("验证数据是否存在>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>id",id)

        if id:
          return jsonify(message="没有参数", code=204)

        coll = CourseModel.query.filter_by(id=pk)
        collect_all = CourseModel.query.filter_by(id=pk).first()
        collect=marshal(collect_all, course_fields)
        collect_json = json.loads(json.dumps(collect))
        print(">>>>>>>>>>>>>>>>>>>>>>SFDSF..》》》》》》》》》》》》》", collect_json)
        if collect_json["collect"] == 0:
            coll.update({"collect": 1, "attention": collect_json["attention"]-1})
            db.session.commit()
            return jsonify(message="取消收藏成功",is_favorite=False, code=200)
        else:
            coll.update({"collect": 0, "attention": collect_json["attention"]+1})
            db.session.commit()
            return jsonify(message="收藏成功",is_favorite=True, code=200)






api.add_resource(CourseCommentResource, "/add_comment")
api.add_resource(CommentLikeResource, "/like_comment")
api.add_resource(CollectResource, "/collect/<int:pk>")











